package com.cts.c2;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.client.RestTemplate;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.ActionBar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ActionBar.Tab;
import android.support.v4.app.ActionBar.TabListener;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.Window;
import android.support.v4.view.MenuItem.OnMenuItemClickListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cts.c2.services.StreamService;
import com.cts.c2.vo.Mystream;
import com.cts.c2.vo.Notification;
import com.cts.c2.vo.Notifications;
import com.cts.c2.vo.SendNotificationVO;
import com.cts.c2.vo.SweetVO;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;

public class StreamActivity extends FragmentActivity implements TabListener {
	private static final int SHOW_CWEET_POST = 1;

	ViewPagerAdapter mAdapter;

	ViewPager mPager;
	TitlePageIndicator mIndicator;
	public static List<SweetVO> prodArrayList;
	public static List<Notification> notificationList = new ArrayList<Notification>();;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_ACTION_BAR_ITEM_TEXT);
		setContentView(R.layout.main);
		// loadCweets();
		mAdapter = new ViewPagerAdapter(this.getBaseContext());
		mPager = (ViewPager) findViewById(R.id.viewpager);
		mPager.setAdapter(mAdapter);
		mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		ActionBar bar = getSupportActionBar();
		bar.setDisplayHomeAsUpEnabled(true);
		// postCweet();
		// testConnection();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("Cweet").setIcon(R.drawable.ic_action_mark)
				.setOnMenuItemClickListener(CweetmenuItemClickListener)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_ALWAYS
								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);

		return super.onCreateOptionsMenu(menu);
	}

	private OnMenuItemClickListener CweetmenuItemClickListener = new OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem item) {
			Intent myIntent = new Intent();
			myIntent.setClassName("com.cts.c2", "com.cts.c2.CweetPostActivity");
			startActivityForResult(myIntent, SHOW_CWEET_POST);
			return true;
		}

	};

	private void testConnection() {
		RestTemplate restTemplate = new RestTemplate();
		// String url =
		// "https://mashupmobilest4.cognizant.com/LDAPService/Service1.asmx/LdapTestWithBBDeviceID?username=175931&pwd=Password-24";
		String url = "https://mashupmobilest4.cognizant.com/MobileNewService/NotificationsandSSDSPermissions.asmx/MyCweetsCommunityByID_New?strCweetURL=Site29;175931&CommunityID=&filter=&pagesize=5&PageNo=1&strUserID=175931&BBDeviceID=12345";
		// String url =
		// "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}";
		Mystream result = restTemplate.getForObject(url, Mystream.class);
		Log.i("response from server", result.getNotifications().getList()
				.get(0).getFromId());
	}

	private void loadCweets() {
		StreamService streamService = new StreamService();
		Notifications notifications;
		Mystream mystream = streamService.getMyStream("", "", "", "", "", "",
				"");
		if (mystream != null) {
			notifications = mystream.getNotifications();
			if (notifications != null)
				notificationList = notifications.getList();
		}
		/*prodArrayList = new ArrayList<SweetVO>(20);
		SweetVO sweetVO;
		for (int i = 0; i < 20; i++) {
			sweetVO = new SweetVO();
			sweetVO.setAuthor("Author " + i);
			sweetVO.setCommentsCnt("4 comments");
			sweetVO.setId("id" + i);
			sweetVO
					.setText("This is really a good move from C2 team. It really encourages all the Android developers. Android developers ... It's your time to showcase."
							+ i);
			sweetVO.setTimeStamp("12/12/2011");
			sweetVO.setTitle("How is this Chalenge..??" + i);
			prodArrayList.add(sweetVO);
		}*/
	}

	private void postCweet() {
		SendNotificationVO notificationVO = new SendNotificationVO();
		notificationVO.setTitle("string");
		notificationVO.setFromId("175931");
		notificationVO.setToId("Cognizant All");
		notificationVO.setContext("");
		notificationVO.setFromName("Penumatsa, Varahala Raju");
		notificationVO.setToName("Cognizant All");
		notificationVO.setStatus("");
		notificationVO.setLastModifiedDate("11/6/2011 9:13:32 AM");
		notificationVO.setEntityType("Cweets");
		notificationVO.setComment("");
		notificationVO.setHide("False");
		notificationVO.setNotificationText("Notification text");
		notificationVO.setNotificationTitle("Test notification title");
		notificationVO.setStartDate("11/6/2011 9:13:32 AM");
		notificationVO.setEndDate("11/6/2011 9:13:32 AM");
		notificationVO.setFeatured("N");
		notificationVO.setItemLink("");
		notificationVO.setCreatedBy("175931");
		notificationVO.setModifiedBy("175931");
		notificationVO.setCommunityName("Cognizant All");
		notificationVO.setCommunityID("O201011100000000");
		notificationVO.setLatest("L");
		notificationVO.setEntityId("");
		notificationVO.setSIPAddress("");
		notificationVO.setBBDeviceID("12345");

		StreamService streamService = new StreamService();
		String result = streamService.sendNotification(notificationVO);
		if (result != null) {
			Log.i("response from server", result);
		}

	}

	public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {
		private String[] titles = new String[] { "My Stream", "Network Stream" };
		private final Context context;

		public ViewPagerAdapter(Context context) {
			this.context = context;
		}

		@Override
		public String getTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Object instantiateItem(View pager, int position) {
			ListView v = new ListView(context);
			v.setAdapter(new CweetsListAdapter(context));
			((ViewPager) pager).addView(v, 0);
			return v;
		}

		@Override
		public void destroyItem(View pager, int position, Object view) {
			((ViewPager) pager).removeView((ListView) view);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view.equals(object);
		}

		@Override
		public void finishUpdate(View view) {
		}

		@Override
		public void restoreState(Parcelable p, ClassLoader c) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View view) {
		}
	}

	public static class CweetsListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		private StreamActivity parent;

		public CweetsListAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return notificationList.size();
		}

		public Object getItem(int position) {
			return notificationList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ListContent holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.row, null);
				holder = new ListContent();
				holder.authName = (TextView) convertView
						.findViewById(R.id.authName);
				holder.cweetTime = (TextView) convertView
						.findViewById(R.id.sweetTime);
				holder.titleTxt = (TextView) convertView
						.findViewById(R.id.titleTxt);
				holder.descTxt = (TextView) convertView
						.findViewById(R.id.descTxt);
				holder.commentsCnt = (TextView) convertView
						.findViewById(R.id.commentsCnt);

				convertView.setTag(holder);
			} else {
				holder = (ListContent) convertView.getTag();
			}

			holder.authName.setText(notificationList.get(position)
					.getFromName());
			holder.cweetTime.setText(notificationList.get(position)
					.getLastModifiedDate());
			holder.titleTxt.setText(notificationList.get(position).getnTitle());
			holder.descTxt.setText(notificationList.get(position).getnText());
			holder.commentsCnt.setText(notificationList.get(position)
					.getCommentCount().toString());
			return convertView;
		}

		static class ListContent {
			TextView authName;
			TextView cweetTime;
			TextView titleTxt;
			TextView descTxt;
			TextView commentsCnt;
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in Action Bar clicked; go home
			Intent intent = new Intent(this, HomeActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

		Toast.makeText(getApplicationContext(), "POst sweet button selected",
				Toast.LENGTH_SHORT).show();
		/*Intent myIntent = new Intent();
		myIntent.setClassName("com.cts.c2",
				"com.cts.c2.C2AndroidActivity");
		startActivityForResult(myIntent, SHOW_HOME);*/
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}
}